<?php
class Controller_Sales_Product extends Controller_Abstract
{
	function actionFloorPriceUpload()
	{
		$this->_view['title'] = '最低售价上传';
	}
	
	function actionFloorPriceUploadTextSave()
	{
		$this->_view['title'] = '保存最低售价（textarea）';
		if ($this->_context->isPOST())
		{
			$data = explode("\n", $_POST['prodcut_floor_price']);
			foreach ($data as $d)
			{
				$product_floor_price = explode(',', trim($d));
				$product = Product_Basic::find('number=?', trim($product_floor_price[0]))->getOne();
				if ($product['id'])
				{
					$product->sales_floor_price = $product_floor_price[1];
					$product->save();
				}
			}
			return $this->_redirectMessage('上传成功', '正在返回..', url('Sales::Product/FloorPriceList'), SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('上传失败', '没有文件', $_SERVER['HTTP_REFERER'], FAILURE);
		}
		
	}
	
	function actionFloorPriceUploadFileSave()
	{
		$this->_view['title'] = '保存最低售价（file）';
		if ($this->_context->isPOST())
		{
			$upload_result = Upload_Files::uploadFile('sales_floor_price', Q::ini('custom_flag/upload_file_type/product_basic/value'));
			if ($upload_result['ack'] == SUCCESS)
			{
				$file_name = $upload_result['path'];
				$handle = fopen($file_name, "r");
			}
			$data = array();
			if ($handle)
			{
				while ( ! feof($handle))
				{
					$data[] = fgets($handle, 4096);
				}
			}
			foreach ($data as $d)
			{
				$product_floor_price = explode(',', trim($d));
				$product = Product_Basic::find('number=?', trim($product_floor_price[0]))->getOne();
				if ($product['id'])
				{
					$product->sales_floor_price = $product_floor_price[1];
					$product->save();
				}
			}
			return $this->_redirectMessage('上传成功', '正在返回..', url('Sales::Product/FloorPriceList'), SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('上传失败', '没有文件', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionFloorPriceList()
	{
		$this->_view['title'] = '列表';
		$shop = Shop::getActiveShop();
		$shop_group = array();
		foreach ($shop as $s)
		{
			$shop_group[$s['platform']][] = $s;
		}
		$this->_view['shop_group'] = $shop_group;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = Product_Floor_Price::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Sales::Product/FloorPriceList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['product_floor_price'] = $result['data'];
		}
	}
	
	function actionMultiUpdateStatus()
	{
		$this->_view['title'] = '批量修改产品销售状态';
		$warehouse = Warehouse::getActiveWarehouse();
		$this->_view['warehouse'] = $warehouse;
	}
	
	function actionCheckNumberExists()
	{
		$this->_view['title'] = '检查产品编号是否有效';
		if (isset($_REQUEST['product_number']) && isset($_REQUEST['warehouse_id']))
		{
			$product_basic_number = Q::normalize($_REQUEST['product_number'], "\n");
			$result = Product_Warehouse::checkExists($product_basic_number, $_REQUEST['warehouse_id']);
			if ($result['ack'] == SUCCESS)
			{
				echo 1;exit;
			}
			else
			{
				echo json_encode($result['not_exists']);exit;
			}
		}
		else
		{
			echo -1;exit;
		}
	}
		
	function actionMultiUpdateStatusSave()
	{
		$this->_view['title'] = '批量修改产品销售状态提交';
		if ($this->_context->isPOST())
		{
			$product_basic_number = isset($_POST['multi_product_basic_number']) ? $_POST['multi_product_basic_number'] : '';
			$sales_status = isset($_POST['sales_status']) ? $_POST['sales_status'] : '';
			$warehouse_id = isset($_POST['warehouse_id']) ? $_POST['warehouse_id'] : '';
			if ($warehouse_id && $product_basic_number && $sales_status)
			{
				$product_basic_number = Q::normalize($product_basic_number, "\n");
				$result = Product_Warehouse::checkExists($product_basic_number, $warehouse_id);
				if ($result['ack'] == SUCCESS)
				{
					Product_Warehouse::multiUpdateProductStatus($product_basic_number, $warehouse_id, 'sales', $sales_status);
					return $this->_redirectMessage('修改成功', '正在返回', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					$product_basic_number = array_diff($product_basic_number, $result['not_exists']);
					if (!empty($product_basic_number))
					{
						Product_Warehouse::multiUpdateProductStatus($product_basic_number, $warehouse_id, 'sales', $sales_status);
					}
					return $this->_redirectMessage('修改失败', '产品编号为'. implode(',', $result['not_exists']) . '不存在', $_SERVER['HTTP_REFERER'], FAILURE);
				}
			}
			else
			{
				return $this->_redirectMessage('修改失败', '仓库或产品编号或状态选择不能为空', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
}